/*
 * AreaOfInterestFrame.java
 *
 * Created on February 19, 2007, 12:04 AM
 */

package edu.byu.isys413.group1E.gui;

import edu.byu.isys413.group1E.*;
import edu.byu.isys413.group1E.data.AreaOfInterestBO;
import edu.byu.isys413.group1E.data.AreaOfInterestDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author  tkulbeth
 */
public class AreaOfInterestFrame extends javax.swing.JFrame {
    
    private MembershipFrame callingMemberFrame = null;
    private CustomerFrame callingCustFrame = null;
    private ArrayList<String> list_AOI_IDs = new ArrayList<String>();
    
    /**
     * Creates new form AreaOfInterestFrame
     */
    public AreaOfInterestFrame() {
        initComponents();
    }
    
    /**
     * Creates new form AreaOfInterestFrame using a frame object parameter
     * to refer control back to the calling object
     */
    public AreaOfInterestFrame(MembershipFrame frame) {
        initComponents();
        callingMemberFrame = frame;
        this.updateListView();
    }
    
    public AreaOfInterestFrame(CustomerFrame frame) {
        initComponents();
        callingCustFrame = frame;
        this.updateListView();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        label_AOIdesc = new javax.swing.JLabel();
        field_AOIdesc = new javax.swing.JTextField();
        button_AddAOI = new javax.swing.JButton();
        label_AOIdesc2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_FullAreasOfInterests = new javax.swing.JList();
        button_CloseAOI = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add New Area of Interest");
        label_AOIdesc.setText("Enter New Area of Interest Description:");

        field_AOIdesc.setText("<Description>");
        field_AOIdesc.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                field_AOIdescFocusGained(evt);
            }
        });

        button_AddAOI.setText("Add");
        button_AddAOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_AddAOIActionPerformed(evt);
            }
        });

        label_AOIdesc2.setText("Current List of Areas of Interests:");

        jScrollPane1.setViewportView(list_FullAreasOfInterests);

        button_CloseAOI.setText("Close");
        button_CloseAOI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_CloseAOIActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(field_AOIdesc, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_AOIdesc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_AddAOI))
                    .addComponent(label_AOIdesc2)
                    .addComponent(button_CloseAOI, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_AOIdesc)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(field_AOIdesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_AddAOI))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_AOIdesc2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(button_CloseAOI)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button_CloseAOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_CloseAOIActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_button_CloseAOIActionPerformed
    
    private void button_AddAOIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_AddAOIActionPerformed
        AreaOfInterestBO aoi;
        if(callingMemberFrame != null){
            try {
                aoi = AreaOfInterestDAO.getInstance().create();                     //create object
                aoi.setDescription(field_AOIdesc.getText());
                AreaOfInterestDAO.getInstance().save(aoi);                          //save to DB
                this.field_AOIdesc.setText("");
                this.updateListView();
                this.callingMemberFrame.updateListView();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }else if(callingCustFrame != null){
            try {
                aoi = AreaOfInterestDAO.getInstance().create();                     //create object
                aoi.setDescription(field_AOIdesc.getText());
                AreaOfInterestDAO.getInstance().save(aoi);                          //save to DB
                this.field_AOIdesc.setText("");
                this.updateListView();
            } catch (Exception ex) {
                ex.printStackTrace();
            }            
        }
    }//GEN-LAST:event_button_AddAOIActionPerformed
    
    private void field_AOIdescFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_field_AOIdescFocusGained
        if(field_AOIdesc.getText().equals("<Description>")) { field_AOIdesc.setText(""); }
    }//GEN-LAST:event_field_AOIdescFocusGained
    
    /*now refresh the list according to what was added in the add AOI frame*/
    public synchronized void updateListView(){
        try {
            ArrayList<AreaOfInterestBO> aoiList = AreaOfInterestDAO.getInstance().readList();
            ArrayList<String> currentList = new ArrayList<String>();      //set up list for storing descriptions
            //build the list for the view
            for(int z=0; z < aoiList.size(); z++){
                currentList.add(aoiList.get(z).getDescription());           //add the description to the list to display
                list_AOI_IDs.add(aoiList.get(z).getDescription());          //keep a list of AOI_IDs for future reference
            }
            
            this.list_FullAreasOfInterests.setListData(currentList.toArray());
            this.list_FullAreasOfInterests.updateUI();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new AreaOfInterestFrame().setVisible(true);
//            }
//        });
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_AddAOI;
    private javax.swing.JButton button_CloseAOI;
    private javax.swing.JTextField field_AOIdesc;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_AOIdesc;
    private javax.swing.JLabel label_AOIdesc2;
    private javax.swing.JList list_FullAreasOfInterests;
    // End of variables declaration//GEN-END:variables
    
}
